const MathLib = require('MathLib').MathLib;
const Coordinate = require('MathLib').Coordinate;
const CalcWorkSpace = require('MathLib').CalcWorkSpace;
const CanvasPlus = require('CanvasPlus').CanvasPlus;

const Config = {
  frequency: 40,
  duration: 0.1,
}
let time = () => { return Math.round((new Date).valueOf() / 1000); }
let random = (s, e)=>{ return Math.round(Math.random() * (e-s) + s); }
let randomColor = () => { return 'rgb(' + random(180, 255) + ',' + random(180, 255) + ',' + random(180, 255) + ')'; }
let randomCircularGradient = (cp)=>{
  let grd = cp.createCircularGradient(2, 2, 2)
  grd.addColorStop(0, 'rgba(255,255,255,0)')
  grd.addColorStop(1, randomColor());
  return grd;
}


function TouchEvent(e){
  let cp = this.cp;
  this.timeStamp = Math.round(e.timeStamp);
  this.changedTouches = e.changedTouches;
  if ( this.changedTouches.length>1 ){
    let x=0,y=0;
    for (let i = 0; i < this.changedTouches.length; i++){
      x += this.changedTouches[i].x;
      y += this.changedTouches[i].y;
    }
    this.changedTouches[0].x = x / this.changedTouches.length;
    this.changedTouches[0].y = y / this.changedTouches.length;
  }
  this.touches = e.touches;
  this.type = e.type;
}

/**
 * touchstart 触摸开始监控器
 */
function touchstart(e) {
  let cp = this.cp;
  let te = new TouchEvent(e);
  this.TouchLib.push(this.LastTouch = te);
  this.Varsion++;

  cp.circle(te.changedTouches[0].x, te.changedTouches[0].y, 5).fill(randomColor());

  setTimeout(() => {
    cp.clearRect(te.changedTouches[0].x - 2, te.changedTouches[0].y - 2, 4, 4);
  }, Config.duration * 1000 + 11);

  setTimeout(() => {
    cp.clearRect(te.changedTouches[0].x - 5.3, te.changedTouches[0].y - 5.3, 10.6, 10.6);
  }, Config.duration * 1000 + 51);

  this.RingStatus = true;
  this.starRing();
}
TouchStar.prototype.touchstart = touchstart;

/**
 * touchend 触摸结束监控器
 */
function touchend(e) {
  let cp = this.cp;
  let te = new TouchEvent(e);
  let v = this.Varsion;

  cp.circle(this.LastTouch.changedTouches[0].x, this.LastTouch.changedTouches[0].y, 2).fill(randomColor());

  setTimeout(() => {
    cp.clearRect(te.changedTouches[0].x - 2, te.changedTouches[0].y - 2, 4, 4);
  }, Config.duration * 1001);

  this.LastTouch = new TouchEvent(e);
  this.TouchLib.push(this.LastTouch);

  setTimeout(() => {
    if (this.Varsion === v) {
      cp.draw(false);
      this.RingStatus = false;
    }

  }, Config.duration * 1001 + 101);

}
TouchStar.prototype.touchend = touchend;

/**
 * touchmove 触摸持续监控器
 */
function touchmove(e) {
  let v = this.Varsion;
  if (this.eventResponseStatus && v === this.Varsion) return;
  setTimeout(() => {
    this.eventResponseStatus = false;
  }, 1000 / Config.frequency);
  this.eventResponseStatus = true;

  let cp = this.cp;
  let te = new TouchEvent(e);
  cp.circle(this.LastTouch.changedTouches[0].x, this.LastTouch.changedTouches[0].y, 2).fill(randomColor());

  setTimeout(() => {
    cp.clearRect(te.changedTouches[0].x - 2, te.changedTouches[0].y - 2, 4, 4);
  }, Config.duration * 1000 + 11);

  this.LastTouch = new TouchEvent(e);
  this.TouchLib.push(this.LastTouch);
}
TouchStar.prototype.touchmove = touchmove;

function starRing(){
  let cp = this.cp;
  setTimeout(()=>{
    if (this.RingStatus) {
      cp.draw(true,()=>{
        this.starRing();
      });
    }
  },1000/(Config.frequency));
}
TouchStar.prototype.starRing = starRing;

function TouchStar(canvasId, Context){
  let canvas = wx.createCanvasContext(canvasId);
  this.cp = new CanvasPlus(canvas);
  this.Context = Context;
  this.Context._TouchStar = (e) => { this[e.type](e); };
  this.TouchLib = [];
  this.Varsion = 0;
}




module.exports.TouchStar = TouchStar;
